package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.LmbCousumablePurchaseDAO;
import com.trkj.dao.jpa.LmbMaterialAcceptanceDAO;
import com.trkj.dao.jpa.LmbPurchaseDetailDAO;
import com.trkj.dao.jpa.LmbStorehouseDAO;
import com.trkj.dao.jpa.LmbSupplierDAO;
import com.trkj.dao.jpa.LmbTbstaffDAO;
import com.trkj.pojos.LmbCousumablePurchase;
import com.trkj.pojos.LmbMaterialAcceptance;
import com.trkj.pojos.LmbPurchaseDetail;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.YzrStorehouse;
import com.trkj.pojos.YzrSupplier;
import com.trkj.vo.LmbCousumablePurchaseVo;
import com.trkj.vo.LmbHcYsVo;
import com.trkj.vo.LmbPurchaseDetailVo;

@Service
@Transactional
public class LmbCouPurYhService {
	@Autowired
	private LmbCousumablePurchaseDAO couPurDAO;	
	@Autowired
	private LmbTbstaffDAO staffDAO;
	@Autowired
	private LmbSupplierDAO supplierDAO;
	@Autowired
	private LmbPurchaseDetailDAO purDetailDAO;
	@Autowired
	private LmbMaterialAcceptanceDAO materialAcceptanceDAO;
	@Autowired
	private LmbOddNumbersService oddNumbersService;
	@Autowired
	private LmbStorehouseDAO storehouseDAO;
	@Autowired
	private IYzrTbstaffDAO yzrTbstaffDAO;
	@Autowired
	private LmbCousumablePurchaseService couPurService;
	/**

	 * 查询所有的采购单

	 * @return

	 */
	public List<LmbCousumablePurchaseVo> findAllCouPur(){
		List<LmbCousumablePurchase> list = (List<LmbCousumablePurchase>) couPurDAO.findAll();	
		return poLmbCouPurList2VoList(list);	
	}
	
	/**

	 * 将耗材采购的po转换成Vo

	 * @param staff

	 * @return

	 */
	private LmbCousumablePurchaseVo cousumablePurchase2Vo(LmbCousumablePurchase coupur){
		LmbCousumablePurchaseVo cousumablePurchaseVo = new LmbCousumablePurchaseVo();
		cousumablePurchaseVo.setCpId(coupur.getCpId());
		System.out.println("coupur.getCpId()为："+coupur.getCpId());
		cousumablePurchaseVo.setCpPlId(coupur.getCpPlId());
		cousumablePurchaseVo.setCpStatus(coupur.getCpStatus());
		cousumablePurchaseVo.setCpTime(coupur.getCpTime());
		cousumablePurchaseVo.setCpRsStatus(coupur.getCpRsStatus());
		BigDecimal sid = coupur.getCpBuyer();	
		if(sid != null){
			long Sid = sid.longValue();
			Tbstaff staff = staffDAO.findOne(Sid);
			cousumablePurchaseVo.setSid(Sid);
			cousumablePurchaseVo.setSname(staff.getStaffName());
		}else{
			cousumablePurchaseVo.setSid(0);
			cousumablePurchaseVo.setSname(null);
		}
		String id = coupur.getCpId();
		System.out.println("id1为:"+id);		
		if(id !=null){
			LmbMaterialAcceptance matAcceptance = materialAcceptanceDAO.findAcceptance(id);		
			if(matAcceptance != null){
				System.out.println("matAcceptance"+matAcceptance);
				System.out.println("matAcceptance.getMatPeople()为："+matAcceptance.getMatPeople());			
				cousumablePurchaseVo.setYresult(matAcceptance.getMatResult());
				cousumablePurchaseVo.setyDate(matAcceptance.getMatDate());
				if(matAcceptance.getMatPeople()!=null){
					long matId = matAcceptance.getMatPeople().longValue();
					System.out.println("matId为："+matId);
					cousumablePurchaseVo.setYsr(matId);
					cousumablePurchaseVo.setYname(staffDAO.findName(matId));					
				}			
			}			
		}		
		return cousumablePurchaseVo;		
	}
	
	/**

	 * 将LmbCousumablePurchase的集合转换成LmbCousumablePurchaseVo的集合

	 * @param list

	 * @return

	 */
	private List<LmbCousumablePurchaseVo> poLmbCouPurList2VoList(List<LmbCousumablePurchase> list){
		if(list == null) return null;
		List<LmbCousumablePurchaseVo> listVo = new ArrayList<>();
		for(LmbCousumablePurchase couPur: list){
			LmbCousumablePurchaseVo couPurVo = cousumablePurchase2Vo(couPur);
			listVo.add(couPurVo);
		}
		return listVo;	
	}
	
	/**

	 * 查询采购详情

	 * @return

	 */
	public List<LmbPurchaseDetailVo> findAllPurDetail(String id){
		List<LmbPurchaseDetail> list = purDetailDAO.findPurchaseDetail(id);
		return poPurchaseDetail2VoList(list);	
	}

	/**

	 * 将耗材采购详情的po转换成Vo

	 * @param staff

	 * @return

	 */
	public LmbPurchaseDetailVo purchaseDetail2Vo(LmbPurchaseDetail purDetail){
		LmbPurchaseDetailVo purchaseDetailVo = new LmbPurchaseDetailVo();
		purchaseDetailVo.setPdId(purDetail.getPdId());		
		purchaseDetailVo.setPdCpId(purDetail.getPdCpId());
		purchaseDetailVo.setPdCId(purDetail.getPdCId());
		purchaseDetailVo.setPdName(purDetail.getPdName());
		purchaseDetailVo.setPdBatchNum(purDetail.getPdBatchNum());
		purchaseDetailVo.setPdNorms(purDetail.getPdNorms());
		purchaseDetailVo.setPdPrice(purDetail.getPdPrice());
		purchaseDetailVo.setPdProductDate(purDetail.getPdProductDate());
		purchaseDetailVo.setPdValidTill(purDetail.getPdValidTill());
		purchaseDetailVo.setPdNum(purDetail.getPdNum());
		String sid = purDetail.getPdSupplier();	
		System.out.println("供应商编号为："+sid);
		if(sid != null){
			YzrSupplier supplier = supplierDAO.findOne(sid);
			purchaseDetailVo.setPdSupplier(sid);
			purchaseDetailVo.setSupSid(sid);
			purchaseDetailVo.setSupName(supplier.getSupName());
		}else{
			purchaseDetailVo.setSupSid(null);
			purchaseDetailVo.setSupName(null);
		}	
		return purchaseDetailVo;			
	}
	
	/**

	 * 将LmbPurchaseDetail的集合转换成LmbPurchaseDetailVo的集合

	 * @param list

	 * @return

	 */
	public List<LmbPurchaseDetailVo> poPurchaseDetail2VoList(List<LmbPurchaseDetail> list){
		if(list == null) return null;
		List<LmbPurchaseDetailVo> listVo = new ArrayList<>();
		for(LmbPurchaseDetail purDetail: list){
			LmbPurchaseDetailVo purDetailVo = purchaseDetail2Vo(purDetail);
			listVo.add(purDetailVo);
		}
		return listVo;	
	}
		
	/**

	 * 批量验收采购单

	 * @param empnos

	 */
	public void editCousumablePurchase(String[] list1,String staffAccount,String result){		
		//将数组转换成集合

		DefaultConversionService service =
				new DefaultConversionService();
		List<String> list = service.convert(list1, List.class);
		for(String s: list){
			LmbCousumablePurchase couPurchase = couPurDAO.findOne(s);
			couPurchase.setCpStatus("已验收");
			couPurchase.setCpRsStatus("未入库");
			couPurDAO.save(couPurchase);
			LmbMaterialAcceptance materialAcceptance = new LmbMaterialAcceptance();
			materialAcceptance.setMatCpId(s);
			materialAcceptance.setMatDate(new Date());
			materialAcceptance.setMatResult(result);
			String n = materialAcceptanceDAO.findMaxID("hy");	
			materialAcceptance.setMatId(oddNumbersService.getId("hy", n));
			if(staffAccount !=null){
				System.out.println("zhanghao:"+staffAccount);
				long staffno = staffDAO.findID(staffAccount);
				System.out.println("员工账号为"+staffno);
				BigDecimal staffno1 = new BigDecimal(staffno); 
				materialAcceptance.setMatPeople(staffno1);	
			}			
			materialAcceptanceDAO.save(materialAcceptance);
		}
	}
	
	public List<LmbCousumablePurchaseVo> findCouPurchaseBySearch(String n,String s){
		List<LmbCousumablePurchase> list = new ArrayList<>();
		if(n.equals("status")){
			list = couPurDAO.findByCpStatus(s);
		}else if(n.equals("PlId")){
			list = couPurDAO.findByCpId(s);
		}else if(n.equals("result")){
			System.out.println("n为："+n);
			System.out.println("s为："+s);
			list = couPurDAO.findResult(s);
			System.out.println("list的长度："+list.size());
		}		 
		return poLmbCouPurList2VoList(list);		
	}
	
	public List<LmbCousumablePurchaseVo> findCouPurchaseByResult(){
		List<LmbCousumablePurchase> list = new ArrayList<>();		
			list = couPurDAO.findResult("不合格");
			System.out.println("list的长度："+list.size());		 
		return poLmbCouPurList2VoList(list);		
	}
		
	public List<LmbPurchaseDetailVo> findCouPurchaseDetailByResults(){
		List<LmbPurchaseDetail> list = new ArrayList<>();		
			list = purDetailDAO.findResult("合格");
			System.out.println("list的长度："+list.size());		 
		return poPurchaseDetail2VoList(list);		
	}
	
	
	public List<LmbPurchaseDetailVo> findCouPurchaseDetailByPdCpId(String pdCpId){
		System.out.println("以被调用");
		System.out.println("pdCpId为:"+pdCpId);
		List<LmbPurchaseDetail> list = new ArrayList<LmbPurchaseDetail>();
		list=purDetailDAO.findDetail(pdCpId);
		return poPurchaseDetail2VoList(list);		
	}
	
	public List<LmbPurchaseDetailVo> findPZDH(String n) {
		List<LmbPurchaseDetailVo> list=findCouPurchaseDetailByPdCpId(n);	
		return list;							
	}
	
	public List<YzrStorehouse> findCkByKind (String kind){
		List<YzrStorehouse> list = storehouseDAO.findByStoreKind(kind);
		return list;		
	};
	
	public void l(){
		// 获取年份
		int year = new java.util.Date().getYear() + 1900;
		//拼接
		String name = "GY-";	
		name = name + year+"0000000001"; 
		System.out.println(name);	
	}
	
	//查询所有未验收的采购订单
	public Map<String,Object> queryAllCouPurNoCheck(int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbCousumablePurchase> list = couPurDAO.findAllCouPurByNoCheck(pr);
		long total = couPurDAO.countAllCouPurByNoCheck();
		//LmbCousumablePurchaseService couPurService = new LmbCousumablePurchaseService();
		Map<String,Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", couPurService.poCouPurList2VoList(list));
		return map;				
	}
	
	
	//根据采购订单查询相对应的详情
	public Map<String,Object> queryAlLPurDetByPdcpId(String pdId,int page,int rows){
		PageRequest pr = new PageRequest(page - 1,rows);
		//获取当前页的记录
		List<LmbPurchaseDetail> list = purDetailDAO.findDetailByPdId(pr, pdId);
		long total = purDetailDAO.countDetailByPdId(pdId);
		System.out.println("长度为："+total);
		//LmbCousumablePurchaseService couPurService = new LmbCousumablePurchaseService();
		Map<String,Object> map = new HashMap<>();
		map.put("total",total);
		map.put("rows", couPurService.poCouPurDetList2VoList(list));
		return map;		
	}
	
	//查询所有的采购订单
	public List<LmbCousumablePurchase> queryAllCgCouPur(){
		List<LmbCousumablePurchase> list = (List<LmbCousumablePurchase>) couPurDAO.findAll();
		return list;
	}
	
	/**
	 * 根据采购日期查询采购订单
	 * @param startDate
	 * @param endDate
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map<String, Object> queryByJCaiGouDate(Date startDate,Date endDate,int page, int rows){
		PageRequest pr = new PageRequest(page - 1, rows);
		
		if(startDate.getTime() > endDate.getTime()){
			Date t = startDate;
			startDate = endDate;
			endDate = t;
		}
		List<LmbCousumablePurchase> list = couPurDAO.findOrderByCiGouDate(startDate, endDate, pr);
		long total = couPurDAO.countOrderByByCiGouDate(startDate, endDate);
		//LmbCousumablePurchaseService couPurService = new LmbCousumablePurchaseService();
		Map<String, Object> map = new HashMap<>();
		map.put("total", total);
		map.put("rows", couPurService.poCouPurList2VoList(list));		
		return map;
	}
	
	
	public Map<String, Object> queryByIf(int page, int rows,String name,String value){
		PageRequest pr = new PageRequest(page - 1, rows);
		List<LmbCousumablePurchase> list = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		//LmbCousumablePurchaseService couPurService = new LmbCousumablePurchaseService();
		if(name.equals("name")){
			Tbstaff staff = yzrTbstaffDAO.findByStaffName(value);
			list = couPurDAO.findOrderByBuyer(new BigDecimal(staff.getStaffNo()), pr);
			long total = couPurDAO.countOrderByBuyer(new BigDecimal(staff.getStaffNo()));
			map.put("total", total);					
		}else if(name.equals("checkState")){
			list = couPurDAO.findCousPurchaseByCpStatus(value, pr);
			long total = couPurDAO.countCousPurchaseByCpStatus(value);
			map.put("total", total);			
		}else if(name.equals("rukuState")){
			list = couPurDAO.findCousPurchaseByCpRsStatus(value, pr);
			long total = couPurDAO.countCousPurchaseByCpRsStatus(value);
			map.put("total", total);
			
		}else if(name.equals("orderId")){
			list = couPurDAO.findCousPurchaseByCpId(value, pr);
			long total = couPurDAO.countCousPurchaseByCpId(value);
			map.put("total", total);	
			
		}
		map.put("rows", couPurService.poCouPurList2VoList(list));		
		return map;
	}
	
	
	public void submitCheckMed(LmbHcYsVo hcYsVo){
		//获取日期
		String date = oddNumbersService.rq();
		//拼接
		String name = "HY-"+date;
		String n = materialAcceptanceDAO.findMaxID(name);	
		String metId = oddNumbersService.getId(name, n);

		LmbMaterialAcceptance met = new LmbMaterialAcceptance();
		BigDecimal staffId2 = hcYsVo.getMaterialAcceptance().getMatPeople();
		met.setMatId(metId);
		met.setMatPeople(staffId2);
		met.setMatDate(hcYsVo.getMaterialAcceptance().getMatDate());
		met.setMatCpId(hcYsVo.getMaterialAcceptance().getMatCpId());
		materialAcceptanceDAO.save(met);
				
		LmbCousumablePurchase couPurchase = couPurDAO.findOne(hcYsVo.getMaterialAcceptance().getMatCpId());
		couPurchase.setCpStatus("已验收");
		couPurDAO.save(couPurchase);
		
		List<LmbPurchaseDetail> list = hcYsVo.getPurchaseDetail();
		for(LmbPurchaseDetail purDetail:list){
			System.out.println(purDetail.getPdNum());
			LmbPurchaseDetail purchaseDetail = purDetailDAO.findOne(purDetail.getPdId());
			purchaseDetail.setPdNum(purDetail.getPdNum());
			purDetailDAO.save(purchaseDetail);
		}		
	}
	
}